package xyz.xiaomuyu.moment_read_app.ui.DBtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import xyz.xiaomuyu.moment_read_app.R;
import xyz.xiaomuyu.moment_read_app.bean.Article;
import xyz.xiaomuyu.moment_read_app.bean.User;
import xyz.xiaomuyu.moment_read_app.util.DBUtil;

public class DBActivity extends AppCompatActivity {
    private Button queryUser;
    private TextView showUser;
    ArrayList<User> userList = new ArrayList<>();
    ArrayList<Article> articleList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dbtest);

        showUser = findViewById(R.id.show_user);
        queryUser = findViewById(R.id.query_user);
        queryUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                queryUser();
                queryArticle();
                show();
            }
        });
    }

    public void queryUser() {
        /**
         * 不支持主线程 进行连接数据库这种耗费时间的操作
         * 所以需要重新开启一个线程
         * @author: xiaomuyu
         */
        final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Connection connection = DBUtil.getConn();
                    Statement statement = connection.createStatement();
                    String sql = "select * from user";
                    ResultSet resultSet = statement.executeQuery(sql);
                    while (resultSet.next()) {
                        User user = new User(resultSet.getString("user_id"),
                                resultSet.getString("user_name"),
                                resultSet.getString("password"),
                                resultSet.getString("head_id"),
                                resultSet.getInt("fish_coin"),
                                resultSet.getInt("fans"));
                        userList.add(user);

                    }
                    statement.close();
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void queryArticle() {
        /**
         * 不支持主线程 进行连接数据库这种耗费时间的操作
         * 所以需要重新开启一个线程
         * @author: xiaomuyu
         */
        final Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Connection connection = DBUtil.getConn();
                    Statement statement = connection.createStatement();
                    String sql = "select * from article";
                    ResultSet resultSet = statement.executeQuery(sql);
                    while (resultSet.next()) {
                        Article article = new Article(resultSet.getString("article_id"),
                                resultSet.getString("name"),
                                resultSet.getString("content"),
                                resultSet.getInt("like_num"),
                                resultSet.getInt("comment_num"),
                                resultSet.getInt("view_num"),
                                resultSet.getTimestamp("create_time")
                        );
                        /**
                         * 关于 MySQL 中datetime类型参数 接收
                         * 这里最后一个参数create_time可以选择不同形式接收：
                         * 1.   resultSet.getDate(), 2021-05-12
                         * 2.   resultSet.getTime(), 21:45:01
                         * 3.   resultSet.getTimestamp() 2021-05-12 21:45:01.0
                         * @author: xiaomuyu
                         */
                        articleList.add(article);
                    }
                    statement.close();
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void show() {
        if (userList.isEmpty()) {
            showUser.setText("there is no data now!!!");
        } else {
            for (User user : userList) {
                showUser.setText(showUser.getText().toString() + "\n" + user.toString());
            }
        }

        if (articleList.isEmpty()) {
            showUser.setText(showUser.getText().toString() + "\n" +"there is no data now!!!");
        } else {
            for (Article article : articleList) {
                showUser.setText(showUser.getText().toString() + "\n" + article.toString());
            }
        }

    }


}